package com.yang.controller;

import com.github.pagehelper.PageInfo;
import com.yang.common.PageQuery;
import com.yang.common.ResponseResult;
import com.yang.entity.dto.PatientDTO;
import com.yang.entity.vo.PatientVO;
import com.yang.entity.vo.RegionVO;
import com.yang.service.PatientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/patient")
@Api(tags = "病人信息相关接口", value = "病人信息相关接口")
@Slf4j
public class PatientController {

    @Autowired
    private PatientService patientService;

    @PostMapping("/pageQuery")
    @ApiOperation(value = "病人信息分页查询")
    public ResponseResult<PageInfo<PatientVO>> pageQuery(@RequestBody PageQuery<PatientDTO> pageQuery) {
        PageInfo<PatientVO> result = patientService.pageQuery(pageQuery);
        return ResponseResult.success(result);
    }

    @GetMapping("/getById")
    @ApiOperation(value = "根据主键查询病人信息")
    public ResponseResult<PatientVO> getById(String id) {
        PatientVO result = patientService.getById(id);
        return ResponseResult.success(result);
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增病人信息")
    public ResponseResult<Integer> add(@RequestBody PatientDTO patientDTO) throws IOException {
        int result = patientService.add(patientDTO);
        if (result == 1) {
            return ResponseResult.success(result);
        }
        return ResponseResult.fail("新增病人信息失败");
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改病人信息")
    public ResponseResult<Integer> update(@RequestBody PatientDTO patientDTO) {
        int result = patientService.update(patientDTO);
        if (result == 1) {
            return ResponseResult.success(result);
        }
        return ResponseResult.fail("修改病人信息失败");
    }

    @GetMapping("/delete")
    @ApiOperation(value = "删除病人信息")
    public ResponseResult<Integer> delete(String id) {
        int result = patientService.delete(id);
        if (result == 1) {
            return ResponseResult.success(result);
        }
        return ResponseResult.fail("删除病人信息失败");
    }

    @GetMapping("/getRegionList")
    @ApiOperation(value = "查询人体部位")
    public ResponseResult<List<RegionVO>> getRegionList() {
        List<RegionVO> result = patientService.getRegionList();
        return ResponseResult.success(result);
    }

    @PostMapping("/uploadPhoto")
    @ApiOperation(value = "上传图片并返回UUID文件名")
    public ResponseResult<String> uploadPhoto(@RequestParam("file") MultipartFile file) {
        String result = patientService.uploadPhoto(file);
        if (StringUtils.isNotEmpty(result)) {
            return ResponseResult.success(result);
        }
        return ResponseResult.fail("照片上传失败");
    }

    @GetMapping("/getPhoto")
    @ApiOperation(value = "根据照片名称获取照片")
    public void getPhoto(@RequestParam String photoName, HttpServletResponse response) {
        patientService.getPhoto(photoName, response);
    }

}
